#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>

RF24 radio(7, 8);//nrf24l01模块初始化
const byte address[6] = "00001";//通信地址

void motor_Init()//电机初始化函数
{
  pinMode(9,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
  pinMode(A4,OUTPUT);
  pinMode(A5,OUTPUT);
  analogWrite(9,0);
  analogWrite(10,0);
  analogWrite(6,0);
  analogWrite(5,0);
  digitalWrite(A0,LOW);
  digitalWrite(A1,LOW);
  digitalWrite(A2,LOW);
  digitalWrite(A3,LOW);
  digitalWrite(A4,LOW);
  digitalWrite(A5,LOW);
  digitalWrite(2,LOW);
  digitalWrite(4,LOW);
}
void setup() {
Serial.begin(9600);//串口初始化，波特率设置为9600
radio.begin();//nef24l01初始化
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();//开始听，就是开始接收
motor_Init();//电机初始化
}
void go()//前进函数
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  digitalWrite(A5,LOW);
  digitalWrite(A4,HIGH);
  //右轮
  digitalWrite(4,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(A2,HIGH);
  digitalWrite(A3,LOW);
}
void left_turn()//左转
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,LOW);
  digitalWrite(A1,HIGH);
  digitalWrite(A5,HIGH);
  digitalWrite(A4,LOW);
  //右轮
  digitalWrite(4,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(A2,HIGH);
  digitalWrite(A3,LOW);
}
void back()//后退
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,LOW);
  digitalWrite(A1,HIGH);
  digitalWrite(A5,HIGH);
  digitalWrite(A4,LOW);
  //右轮
  digitalWrite(4,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(A2,LOW);
  digitalWrite(A3,HIGH);
}
void right_turn()//右转
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  digitalWrite(A5,LOW);
  digitalWrite(A4,HIGH);
  //右轮
  digitalWrite(4,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(A2,LOW);
  digitalWrite(A3,HIGH);
}
void left()//左平移
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,LOW);
  digitalWrite(A1,HIGH);
  digitalWrite(A5,LOW);
  digitalWrite(A4,HIGH);
  //右轮
  digitalWrite(4,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(A2,LOW);
  digitalWrite(A3,HIGH);
}
void right()//右平移
{
  analogWrite(9,180);
  analogWrite(10,180);
  analogWrite(6,180);
  analogWrite(5,180);
  //左轮
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  digitalWrite(A5,HIGH);
  digitalWrite(A4,LOW);
  //右轮
  digitalWrite(4,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(A2,HIGH);
  digitalWrite(A3,LOW);
}
void stop()//停止
{
  analogWrite(9,0);
  analogWrite(10,0);
  analogWrite(6,0);
  analogWrite(5,0);
  digitalWrite(A0,LOW);
  digitalWrite(A1,LOW);
  digitalWrite(A2,LOW);
  digitalWrite(A3,LOW);
  digitalWrite(A4,LOW);
  digitalWrite(A5,LOW);
  digitalWrite(2,LOW);
  digitalWrite(4,LOW);
}
char text[32] ="";

void loop() {
if(radio.available()){
radio.read(&text, sizeof(text));//读取nrf24l01模块数据
      if(text[0]=='a')//判断text的第一个数据是不是为a
      {
        go();delay(80);//为a就前进80毫秒
        text[32] ="";//将数据变为空
      }
      else  if(text[0]=='b')//判断text的第一个数据是不是为b
      {
        back();delay(80);//为a就前进80毫秒
        text[32] ="";//将数据变为空
      }
      else if(text[0]=='c')//判断text的第一个数据是不是为c
      {
        left();delay(80);//为b就后退80毫秒
        text[32] ="";//将数据变为空
      }
      else if(text[0]=='d')//判断text的第一个数据是不是为d
      {
        right();delay(80);//为d就右转80毫秒
        text[32] ="";//将数据变为空
      }
      else if(text[0]=='e')//判断text的第一个数据是不是为e
      {
        left_turn();delay(150);//为e就左平移毫秒
        text[32] ="";//将数据变为空
      }
      else if(text[0]=='f')//判断text的第一个数据是不是为f
      {
        right_turn();delay(150);//为f就右转80毫秒
        text[32] ="";//将数据变为空
      }
      else//否则
      {
        stop();//停止
        text[32] ="";//将数据变为空
      }
  text[32] ="";//将数据变为空
  }
}